<template>
  <div class="cy-table">
    <div class="header">
      <slot name="header">
        <div class="title">
          {{ contentConfig.title }}
        </div>
        <div class="handler">
          <slot name="handler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="dataList"
      border
      style="width: 100%"
      row-key="id"
      :tree-props="childrenProps.treeProps"
    >
      <el-table-column
        v-if="contentConfig.isShowSelection"
        type="selection"
        width="40px"
        align="center"
      ></el-table-column>
      <el-table-column
        v-if="contentConfig.isShowIndex"
        type="index"
        label="序号"
        width="60px"
        align="center"
      ></el-table-column>
      <template v-for="item in contentConfig.propList" :key="item.prop">
        <el-table-column v-bind="item" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="item.slotName" :row="scope.row">{{
              scope.row[item.prop]
            }}</slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="isShowFooter">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[5, 10, 30]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    dataList: {
      type: Object,
      required: true
    },
    listCount: {
      type: Number,
      default: 0
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 5 })
    },
    isShowFooter: {
      type: Boolean,
      default: () => true
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    contentConfig: {
      type: Object,
      required: true
    }
  },
  emits: ['update:page'],
  setup(props, { emit }) {
    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }

    return {
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped lang="less">
.cy-table {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 5px;
    margin-bottom: 5px;
    .title {
      font-weight: 700;
      font-size: 20px;
    }
  }
  .footer {
    margin-top: 15px;
    .el-pagination {
      text-align: right;
    }
  }
}
</style>
